<?

/******************************************************************************
 ***
 *** PHP Sub Class Document
 *** Name:	System::FileTree.inc
 *** Owner:	eMobile Communications, LLC (Enrique A. Reyes-Otero)
 *** Developer:	Enrique A. Reyes-Otero
 *** Support:	support@emobilecommunications.com
 *** Copyright:	2007 © PHP System API
 *** Licensing: This document is governed by the eMobile Communications System
 ***		License found at in the System directory parallel to
 ***		this document.
 *** Legal:	eMobileCommunications.COM is the prorietor owner of this
 ***		document and the contents there in. If you have not been
 ***		authorized access to this document or its functionality then
 ***		your are advised to remove this document from your possesion.
 ***		Modification of this document without eMobileCommunications.COM
 ***		legal consent or any illegal or misuse of this document or the
 ***		contents and the functionality therein shall be prosecuted to
 ***		the full extent of the law.
 ***
 ******************************************************************************/

class FileTree {

	function FileTree ($root, $path = "", $depth = 1) {

		$root = preg_replace ("/\/\.\./", "", $root);
		$path = preg_replace ("/\/\.\./", "", $path);
		$this->root = new FTDirectory (
				array (root=>$root,path=>$path,depth=>1),
				$depth);
	}
};

class FTDirectory {

	var $nodes = array ();

	function FTDirectory ($stat, $depth) {

		foreach ($stat as $key => $val) $this->$key = $val;
		$this->open_dir = opendir ($this->root . $this->path);
		if ($this->depth++ > $depth) return;
		$this->dir = array ();

		while ($entry = readdir ($this->open_dir)) {

			array_push ($this->dir, $entry);
		}

		sort ($this->dir);

		foreach ($this->dir as $entry) {

			if (preg_match ("/^(\.|\.\.)$/", $entry)) continue;

			$stat = lstat ($this->root . "/" . $this->path . "/" . $entry);

			$amode = sprintf ("%o", $stat["mode"] & 00007);
			$amode = $amode ? $amode : "0";
			$gmode = sprintf ("%o", $stat["mode"] & 00070);
			$gmode = $gmode ? $gmode : "0";
			$umode = sprintf ("%o", $stat["mode"] & 00700);
			$umode = $umode ? $umode : "0";
			$smode = sprintf ("%o", $stat["mode"] & 07000);
			$smode = $smode ? $smode : "0";

			$d_smode = substr ($smode, 0, 1);
			$d_umode = substr ($umode, 0, 1);
			$d_gmode = substr ($gmode, 0, 1);
			$d_amode = substr ($amode, 0, 1);

			#$mode = join (":", array ($d_amode, $d_gmode,
				#		$d_umode, $d_smode));

			$mode = $d_smode.$d_umode.$d_gmode.$d_amode;
			#print $mode."<BR>";

			$sz_mode = strlen ($mode);

			while ($sz_mode--) {

				$smode.=substr ($mode, $sz_mode, 1);
			}

			#print $smode."<BR>";

			$modes = array (
				0 => "---",
				1 => "--x",
				2 => "-w-",
				3 => "r--",
				4 => "r-x",
				5 => "rw-",
				6 => "-wx",
				7 => "rwx"
			);

			$stat = array (
				name => $entry,
				path => $this->path . "/" . $entry,
				root => $this->root,
				depth => $this->depth,
				amode => $modes[substr ($smode, 0, 1)],
				gmode => $modes[substr ($smode, 1, 1)],
				umode => $modes[substr ($smode, 2, 1)],
				d_smode => $d_smode,
				d_amode => $d_amode,
				d_gmode => $d_gmode,
				d_umode => $d_umode,
				size => $stat["size"],
				atime => $this->time_stamp ($stat["atime"]),
				mtime => $this->time_stamp ($stat["mtime"]),
				ctime => $this->time_stamp ($stat["ctime"])
			);

			$file = $this->root . "/" . $this->path . "/" . $entry;
			if (is_link ($file)) {

					if (is_dir (readlink ($file)))
						 $stat["type"] = "directory_link";
					else $stat["type"] = "link";
				$child = new FTLink ($stat);
			} elseif (is_dir ($file)) {

					$stat["type"] = "directory";
					$child = new FTDirectory ($stat, $depth);
			} elseif (is_executable ($file)) {
			
					$stat["type"] = "exec";
				$child = new FTFile ($stat);
			} elseif (is_file ($file)) {
			
					$stat["type"] = "file";
				$child = new FTFile ($stat);
			}

			array_push ($this->nodes, $child);
		}
	}

	function time_stamp ($time) {

		$time = localtime ($time);

		return ($time[5]+1900)."-".

			($time[4]<10?($time[4]==9?"":"0").($time[4]+1):$time[4]+1).
			"-".($time[3]<10?"0".$time[3]:$time[3]).
			" ".($time[2]<10?"0".$time[2]:$time[2]).
			":".($time[1]<10?"0".$time[1]:$time[1]).
			":".($time[0]<10?"0".$time[0]:$time[0]);
	}
};

class FTFile {

	function FTFile ($stat) {

		foreach ($stat as $key => $val) $this->$key = $val;
	}
};

class FTLink {

	function FTLink ($stat) {

		foreach ($stat as $key => $val) $this->$key = $val;
	}
};

?>
